<?php
namespace Admin\Model;
use Think\Model;
class CardTypeModel extends Model 
{
	protected $insertFields = array('card_id','name','company_id','desc');
	protected $updateFields = array('id','card_id','name','company_id','desc');
	protected $_validate = array(
		array('name', 'require', '卡项分类名不能为空！', 1, 'regex', 3),
		array('name', '1,255', '卡项分类名的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('desc', 'require', '卡项类型描述不能为空！', 1, 'regex', 3),
		array('desc', '1,255', '卡项类型描述的值最长不能超过 255 个字符！', 1, 'length', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($name = I('get.name'))
			$where['name'] = array('like', "%$name%");
		if($desc = I('get.desc'))
			$where['desc'] = array('like', "%$desc%");
        //对应瑜伽馆
        if(intval(session('admin.company_id')) !==1){
            $where['company_id'] = ['eq',session('admin.company_id')];
        }
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
        $data['company_id'] = session('admin.company_id');
    }
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}